﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using RequestEchoNest.Data;
using System.Windows.Threading;

namespace RequestEchoNest
{
    internal class MainViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<ArtistModel> _artists = new ObservableCollection<ArtistModel>();


        private MusicCatalog MusicCatalog
        {
            get
            {
                if (_musicCatalog == null)
                {
                    _musicCatalog = new MusicCatalog(Settings.Default.DeveloperKey); 
                }
                return _musicCatalog;
            }

        }


        private CurrentTab _tab;

        private string _cacheFilePath;

        private bool _isBusy;
        private ObservableCollection<string> _songs =new ObservableCollection<string>();
        private MusicCatalog _musicCatalog;

        public ObservableCollection<ArtistModel> Artists
        {
            get { return _artists; }
            set
            {
                _artists = value;
                NotifyPropertyChanged("Artists");
            }
        }

        public ObservableCollection<string> Songs
        {
            get { return _songs; }
            set
            {
                _songs = value;
                NotifyPropertyChanged("Artists");
            }
        }




        public bool IsBusy
        {
            get { return _isBusy; }
            set 
            { 
                _isBusy = value;
                NotifyPropertyChanged("IsBusy");
            }
        }


        public void GetSimilars(string artistName)
        {
            IsBusy = true;
            MusicCatalog.LoadingSimilarArtistsCompleted += catalog_LoadingSimilarArtistsCompleted;
            MusicCatalog.LoadSimilarArtists(artistName);
        }

        protected void ArtistsInvoke(object state)
        {
            var e = (ArtistsLoadedEventArgs)state;
            Artists.Clear();
            Artists.AddRange(e.SimilarArtists);
        }

        void catalog_LoadingSimilarArtistsCompleted(object sender, ArtistsLoadedEventArgs e)
        {
            MusicCatalog.LoadingSimilarArtistsCompleted -= catalog_LoadingSimilarArtistsCompleted;
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                                    (ThreadStart)delegate
                                                      {
                                                          Artists.Clear();
                                                          Artists.AddRange(e.SimilarArtists);
                                                          IsBusy = false;
                                                      });
        }





        private void NotifyPropertyChanged(string property)
        {
            var link = PropertyChanged;
            if (link != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
        }


        public event PropertyChangedEventHandler PropertyChanged;

        internal void GetSimilarTracks(string fileName)
        {
            IsBusy = true;
            MusicCatalog.LoadingSimilarTracksCompleted += catalog_SimilarTracksCompleted;
            MusicCatalog.LoadSimilarSongsForTrack(new java.io.File(fileName));
        }



        private void catalog_SimilarTracksCompleted(object sender, SimilarTracksEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                        (ThreadStart)delegate
                        {
                            _songs.Clear();
                            _songs.AddRange(e.Similars);
                            IsBusy = false;
                        });
            MusicCatalog.LoadingSimilarTracksCompleted -= catalog_SimilarTracksCompleted;
        }
    }
}
